package com.eastjet.equipment.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastjet.equipment.service.DeviceInspectionprojectService;
import com.eastjet.equipment.service.DevicePmtsService;
import com.eastjet.equipment.service.DeviceRulesService;
import com.eastjet.equipment.util.CodeRules;
import com.eastjet.maintenance.domain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/spotcheckPro")
public class DeviceSpotcheckprojectController {
    @Autowired
    private DevicePmtsService pmtsService;

    @Autowired
    private CodeRules codeRules;
    @Autowired
    private DeviceRulesService deviceRulesService;
    private String code;



    @GetMapping("/selectPmts")
    public Page<DevicePmts> getSupplierList(@RequestParam(defaultValue = "1") int page,
                                            @RequestParam(defaultValue = "2") int size,
                                            @RequestParam(defaultValue ="") String condition
    ) {
        System.out.println("condition:" + condition);
        System.out.println("page:"+page);
        return  pmtsService.getPmtsList(page, size,condition);
    }
    @GetMapping("/updateState")
    public int updateState(@RequestParam(value = "id", required = false, defaultValue = "0") Integer id, @RequestParam(value = "status", required = false, defaultValue = "0") Integer status) {

        System.out.println("status:" + status);
        System.out.println("id:" + id);

        int update = pmtsService.updateState(id, status);
        return update;
    }

    @GetMapping("/delPro")
    public int delPro(@RequestParam(value = "id", required = false, defaultValue = "0") Integer id, @RequestParam(value = "isDelete", required = false, defaultValue = "0") Integer isDelete) {

        System.out.println("isDelete:" + isDelete);
        System.out.println("id:" + id);

        int del = pmtsService.del(id, isDelete);
        return del;
    }

    public String code() {
        List<DeviceRules> list = deviceRulesService.getDeviceRulesByTableName("device_pmts");
        String s = codeRules.codeRules(list);
        return s;
    }


    @RequestMapping("PmtsQuery")
    public List<DevicePmts> query(){
        return pmtsService.list(null);
    }
    @RequestMapping("/addPmts")
    public String addInspectionPro(@RequestParam(defaultValue = "") Map<String, Object> map) {

        DevicePmts dp = new DevicePmts();
        dp.setPmtsState(Integer.valueOf(map.get("pmtsState").toString()));
        dp.setPmtsName((String) map.get("pmtsName"));
        dp.setPmtsPartId((String) map.get("pmtsPartId"));
        dp.setPmtsMethod((String) map.get("pmtsMethod"));
        dp.setPmtsContent((String) map.get("pmtsContent"));
        dp.setPmtsCalibration((String) map.get("pmtsCalibration"));
        dp.setPmtsResultId(Integer.valueOf(map.get("pmtsResultId").toString()));
        dp.setPmtsIsresult((String) map.get("pmtsIsresult"));
        dp.setIsDelete(0);
        dp.setPmtsNotes((String) map.get("notes"));
        dp.setCode(code());
        dp.setInsertTime(new Date());
        dp.setUpdateTime(new Date());

        System.out.println("dop:" + dp);
        return pmtsService.projectAdd(dp);
    }

    @RequestMapping("/addSe")
    public String addSe(@RequestParam(defaultValue = "") Map<String, Object> map) {
        System.out.println("se:" + map);
        SelectType st = new SelectType();
        st.setOptions((String) map.get("value"));
        st.setCode(code());
        switch (Integer.valueOf((String) map.get("choose"))) {
            case 1:
                st.setChoose(0);
                break;
            case 2:
                st.setChoose(0);
                break;
            case 3:
                st.setChoose(1);
                st.setJudgment(Integer.valueOf((String) map.get("jugement")));
                break;
            case 4:
                st.setChoose(2);
                st.setJudgment(2);
                break;
            default:
                st.setChoose(0);
                break;
        }
        return pmtsService.SelectAdd(st);
    }

    @RequestMapping("/addRes")
    public String addRes(@RequestParam(defaultValue = "") Map<String, Object> map) {
        System.out.println("res:" + map);
        DeviceInspectiontaskResult sr = new DeviceInspectiontaskResult();
        sr.setIsDelete(0);
        sr.setCode(code());
        switch (Integer.valueOf((String) map.get("choose"))) {
            case 1:
                sr.setChoose(0);
                sr.setLowerLimit(Integer.valueOf((String) map.get("resultLower")));
                sr.setUpperLimit(Integer.valueOf((String) map.get("resultUpper")));
                sr.setResultName((String) map.get("resultName1"));
                break;
            case 2:
                sr.setChoose(0);
                sr.setNotse((String) map.get("notse"));
                sr.setResultName((String) map.get("resultName2"));
                break;
            case 3:
                sr.setChoose(1);
                sr.setResultName((String) map.get("resultName3"));
                break;
            case 4:
                sr.setChoose(2);
                sr.setResultName((String) map.get("resultName4"));
                break;
            default:
                sr.setChoose(0);
                break;
        }

        System.out.println("addres:" + sr);
        return pmtsService.resultAdd(sr);
    }

    @RequestMapping("/update")
    public String update(@RequestBody Map<String, Object> map) {
        System.out.println("update:" + map);
        DevicePmts devicePmts = new DevicePmts();
        devicePmts.setPmtsName((String) map.get("pmtsName"));
        devicePmts.setPmtsState(Integer.valueOf((Integer) map.get("pmtsState")));
        devicePmts.setPmtsNotes((String) map.get("notes"));
        devicePmts.setPmtsContent((String) map.get("pmtsContent"));
        devicePmts.setPmtsPartId((String) map.get("pmtsPartId"));
        devicePmts.setPmtsMethod((String) map.get("pmtsMethod"));
        devicePmts.setPmtsCalibration((String) map.get("pmtsCalibration"));
        devicePmts.setUpdatePerson((String) map.get("updatePerson"));
        devicePmts.setCode((String) map.get("code"));
        return pmtsService.update(devicePmts);
    }

}
